package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.List;
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import java.util.Collection; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinMonHocUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
 }

    public int getDotDKId() {
       return dotDKId;
 }

    private TblDKMHDotDKService tblDKMHDotDKService; 

    private int monHocId; 
    public void setMonHocId(int monHocId) {
       this.monHocId = monHocId;
 }

    public int getMonHocId() {
       return monHocId;
 }

    private TblCTDTMonHocService tblCTDTMonHocService; 

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private TblCanBoService tblCanBoService; 

    private String ngayCapNhat; 
    public void setNgayCapNhat(String ngayCapNhat) {
       this.ngayCapNhat = ngayCapNhat;
 }

    public String getNgayCapNhat() {
       return ngayCapNhat;
 }

    private TblThongTinMonHoc tblThongTinMonHoc; 
    public void setTblThongTinMonHoc (TblThongTinMonHoc tblThongTinMonHoc) {
        this.tblThongTinMonHoc = tblThongTinMonHoc;
 }

    private TblThongTinMonHocService tblThongTinMonHocService; 

	private int preUpdate(Message msg, TblThongTinMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblThongTinMonHoc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
	 
	 
	 private String maMonHoc;
	 
 public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}

public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_MonHoc", "sua")){ 
        return "noprivilege"; 
    } 
        tblThongTinMonHocService = (TblThongTinMonHocService) context.getBean("tbl_ThongTin_MonHocClient"); 
        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient"); 

        tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        msg = new Message(); 
        tblThongTinMonHoc = tblThongTinMonHocService.gettblThongTinMonHoc(new Long(id)); 
        tblThongTinMonHoc.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId))); 

        
        
        //get mon hoc theo ma mon hoc
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        {
        	 ConditionReport con = new ConditionReport(); 
             con.setCol("ma");
             con.setValue1(maMonHoc);
             con.setOperator("eq");
             cons.add(con);	
        }
        List<TblCTDTMonHoc> monHocDK = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 1, 1);

        if(monHocDK != null)
        	tblThongTinMonHoc.setMonHocId(monHocDK.get(0)); 
        else
        	return Action.ERROR;
        
        tblThongTinMonHoc.setCanBoId(tblCanBoService.gettblCanBo(new Long(canBoId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayCapNhat); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblThongTinMonHoc.setNgayCapNhat(d); 
        preUpdate(msg,tblThongTinMonHoc); 
       if (msg.isPreMessage_b()) 
       { 
           tblThongTinMonHocService.updatetblThongTinMonHoc(tblThongTinMonHoc); 
           postUpdate(msg, tblThongTinMonHoc); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_ThongTin_MonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
